<script lang="ts" setup>
import { deletePatientByIdAPI, getPatientMylistAPI } from "@/apis/patient";
import { useConsultStore } from "@/stores/consult";
import type { GetPatientMylistItem } from "@/types/patient";
import { onReady, onShow } from "@dcloudio/uni-app";
import { ref } from "vue";

const swipeOptions = ref([
  {
    text: "删除",
    style: {
      backgroundColor: "#dd524d",
      color: "#fff",
      fontSize: "30rpx",
    },
  },
]);

// 患者列表 最多可添加6人
const patientList = ref<GetPatientMylistItem[]>([]);

// 获取患者列表
const getPatientMylist = async () => {
  const res = await getPatientMylistAPI();
  patientList.value = res.data;
};

// 页面 onShow 显示时，获取患者列表
// onShow 页面显示触发，可触发多次
// onLoad 页面加载触发，只触发一次
onShow(() => {
  getPatientMylist();
});

// 删除患者
const onSwipeActionClick = async (id: string, index: number) => {
  // 前端删除
  patientList.value.splice(index, 1);
  // 后端删除
  await deletePatientByIdAPI(id);
  // 提示
  uni.showToast({ title: "删除成功", icon: "success" });

  // 如果删除的是当前选中的患者，需清空 store 中的 patientId
  if (store.patientId === id) {
    store.patientId = "";
  }
};

//获取页面参数
const props = defineProps<{
  type?: string;
}>();

onReady(() => {
  //动态设置标题写到onReady中
  uni.setNavigationBarTitle({
    title: props.type === "consult" ? "选择患者" : "患者列表",
  });
});

// 选择患者
const store = useConsultStore();
const handleSelectPatient = (id: string) => {
  // 如果不是选择患者，则不进行操作
  if (props.type !== "consult") return;
  // 保存患者 id 到 store
  store.patientId = id;
  console.log(store.patientId);
};

// 跳转到支付页面
// 点击下一步，跳转至支付页面
const handleNextStep = () => {
  uni.navigateTo({
    url: '/subpkg_consult/pages/payment/payment',
  })
}

</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="(item, index) in patientList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onSwipeActionClick(item.id, index)"
        >
          <!-- 
            患者高亮的两种情况：
              选择患者：根据 patientId 高亮
              患者列表：根据 defaultFlag 高亮
          -->

          <view
            class="archive-card"
            @click="handleSelectPatient(item.id)"
            :class="{
              active:
                props.type === 'consult'
                  ? item.id === store.patientId
                  : item.defaultFlag === 1,
            }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">
                <!-- 身份证号 替换部分敏感信息 -->
                {{ item.idCard.replace(/^(.{6}).+(.{4})$/, "$1********$2") }}
              </text>
              <text v-if="item.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/pages/form/form?id=${item.id}`"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/pages/form/form"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>

      <!-- 下一步按钮 -->
      <view class="next-step" v-if="props.type === 'consult'">
        <button 
        :disabled="store.patientId === ''" 
        @click="handleNextStep"
        class="uni-button"
        >
          下一步
        </button>
      </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import "./index.scss";
</style>
